package com.xidian.zhiancloud.controller.riskcontrol.riskpointslist;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TEquipFacStatusList;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaEntity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaGY00Entity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaSS00Entity;
import com.xidian.zhiancloud.model.riskcontrol.entity.TEquipFacStaZY00Entity;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.riskcontrol.riskidenva.TRiskAssessmentMeasuresService;
import com.xidian.zhiancloud.service.riskcontrol.riskidenva.TRiskCheckItemsService;
import com.xidian.zhiancloud.service.riskcontrol.riskidenva.TRiskJobCheckItemService;
import com.xidian.zhiancloud.service.riskcontrol.riskidenva.TRiskProcessInspectionItemService;
import com.xidian.zhiancloud.service.riskcontrol.riskpointslist.TEquipFacStatusService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import com.xidian.zhiancloud.util.tools.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 风险管控-风险点清单
 */
@RestController
@RequestMapping("/teqfacsta")
public class TEquipFacStatusController extends BaseController {

    @Autowired
    TEquipFacStatusService tEquipFacStatusService;

    //风险管控-风险点清单-设备设施清单-检查项
    @Autowired
    TRiskCheckItemsService tRiskCheckItemsService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    TRiskAssessmentMeasuresService tRiskAssessmentMeasuresService;

    //作业活动清单-检查项
    @Autowired
    TRiskJobCheckItemService tRiskJobCheckItemService;

    //工艺节点-检查项
    @Autowired
    TRiskProcessInspectionItemService tRiskProcessInspectionItemService;

    /**
     * 风险点清单列表
     * @param reqdata 参数集合
     * @param equipType 设施设备类型
     * @param riskpointType 风险点清单类型
     * @param  status 是否评价过 : 1 : 评价过 ; 0 : 未评价 ;
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTEquipFacStatusList")
    public ResultDTO getTEquipFacStatusList
        (BaseReqQuery reqdata,@RequestParam("riskpointType") String riskpointType,
           @RequestParam(value = "equipType",required = false) Integer equipType,
           @RequestParam(value = "status",required = false) Integer status,
         @RequestParam(value = "administerId",required = false)
             Integer administerId){
        Long companyId=this.getSessionUserCompanyId();
        Page<TEquipFacStatusList> pagedata = tEquipFacStatusService
            .getTEquipFacStatusList(reqdata,equipType,riskpointType,companyId,status,administerId);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("datalist", pagedata);
        data.put("PageNum", pagedata.getPageNum());
        data.put("PageSize", pagedata.getPageSize());
        data.put("PageTotal", pagedata.getPages());
        data.put("total", pagedata.getTotal());
        return ResultDTO.successOf(data, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加风险点清单
     * @param riskpointType 风险点清单类型 : ss00 : 设备设施清单 ;
     *        zy00 : 作业活动清单 ; gy00 : 工艺节点清单 ;
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/addTEquipFacStatusList")
    public ResultDTO addTEquipFacStatusList(
         @RequestParam("riskpointType")String riskpointType,
         TEquipFacStatusList tefsdata){
        Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat
                ("yyyy-MM-dd HH:mm:ss");
        System.out.println(" sbf : "+sbf.format(d));
        //去除字符串时间的横杠、空格、冒号
        String  risknum= sbf.format(d).
                replaceAll("[[\\s-:punct:]]","");
        if(StringUtils.isNotBlank(riskpointType) ||
                riskpointType.equals("ss00")){
            tefsdata.setRiskNum("SS"+risknum);
        }else if(StringUtils.isNotBlank(riskpointType) ||
                riskpointType.equals("zy00")){
            tefsdata.setRiskNum("ZY"+risknum);
        }else if(StringUtils.isNotBlank(riskpointType) ||
                riskpointType.equals("gy00")){
            tefsdata.setRiskNum("GY"+risknum);
        }
        tefsdata.setCompanyId(this.getSessionUserCompanyId());
        tefsdata.setAddBy(this.getSessionUserid().toString());
        tEquipFacStatusService.insertTEquipFacStatusList(tefsdata);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除
     * @param id 风险点清单ID
     * @param ids 检查项ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/deleteTEquipFacStatus")
    public ResultDTO deleteTEquipFacStatus(
            @RequestParam(value="id") String id,
            @RequestParam(value="ids",required = false,
                    defaultValue = "0") String ids){
        return ResultDTO.successOf(tEquipFacStatusService.
                deleteTEquipFacStatus(id,ids),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 修改
     * @param id 风险点清单ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/modifyTEquipFacStatus")
    public ResultDTO modifyTEquipFacStatus
        (@RequestParam(value="id") Long id,TEquipFacStatusList tefsdata) {
        return ResultDTO.successOf(tEquipFacStatusService.modifyTEquipFacStatus
                (id,tefsdata),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 查看
     * @param id 风险点清单ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/viewTEquipFacStatus")
    public ResultDTO viewTEquipFacStatus(@RequestParam(value="id") String id){
        Long companyId=this.getSessionUserCompanyId();
        List<TEquipFacStatusList> tEquipFacStatus = tEquipFacStatusService.
                getById(companyId,id);
        Map<String, Object> resuMap = new HashMap();
        resuMap.put("tEquipFacStatus", tEquipFacStatus);//风险点清单
        return ResultDTO.successOf(resuMap,GeneralResponseEnums.SUCCESS);
    }

    /**
     *  -导出风险点清单列表
     */
    @GetMapping("/exportEquipFacStaData")
    public void exportEquipFacStaData (HttpServletResponse response,
         @RequestParam("riskpointType") String riskpointType,
         @RequestParam(value="id",required=false ,defaultValue="0") String id)
            throws IOException {
        Long companyId=this.getSessionUserCompanyId();
        Page<TEquipFacStaEntity> pagedata = tEquipFacStatusService
                .getExportEquipFacStaData(companyId,riskpointType,id);
        List<TEquipFacStaEntity> list = pagedata.getResult();
        ExcelUtil2<TEquipFacStaEntity> util = new ExcelUtil2<TEquipFacStaEntity>
                (TEquipFacStaEntity.class);
        util.exportExcel(response, list, "风险点清单");
    }

    /**
     * 导入设备设施风险点清单
     * @param file 上传文件
     * @return
     */
    @RequestMapping("/ImportSS00ExcelFile")
    @ResponseBody
    public ResultDTO ImportSS00ExcelFile(@RequestParam MultipartFile file) {
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        try{
            Object obj = TEquipFacStaSS00Entity.class.newInstance();
            List<Object> list = ExcelUtils.importBaseExcel1(file,
                "设备设施导入测试",obj);
            List<TEquipFacStaSS00Entity> ss00ExcelList = new ArrayList<>();
            int index = 2;
            for(int i=index;i<list.size();i++) {

                TEquipFacStaSS00Entity ss00Ex =
                    (TEquipFacStaSS00Entity)list.get(i);
                
                ss00Ex.setEquipNum(((TEquipFacStaSS00Entity) list.get(i)).getEquipNum());
                ss00Ex.setEquipType(((TEquipFacStaSS00Entity) list.get(i)).getEquipType());
                ss00Ex.setEquipName(((TEquipFacStaSS00Entity) list.get(i)).getEquipName());
                ss00Ex.setMode(((TEquipFacStaSS00Entity) list.get(i)).getMode());
                ss00Ex.setPositionId(((TEquipFacStaSS00Entity) list.get(i)).getPositionId());
                ss00Ex.setAdministerId(((TEquipFacStaSS00Entity) list.get(i)).getAdministerId());
                ss00Ex.setEquipSp(((TEquipFacStaSS00Entity) list.get(i)).getEquipSp());
                ss00Ex.setRemarks(((TEquipFacStaSS00Entity) list.get(i)).getRemarks());
                ss00Ex.setPicture(((TEquipFacStaSS00Entity) list.get(i)).getPicture());

                ss00Ex.setInspectionItems(((TEquipFacStaSS00Entity) list.get(i))
                    .getInspectionItems());
                ss00Ex.setInspectionStandard(((TEquipFacStaSS00Entity) list.get(i))
                    .getInspectionStandard());
                ss00Ex.setNonCompliance(((TEquipFacStaSS00Entity) list.get(i))
                    .getNonCompliance());
                ss00Ex.setMainConsequences(((TEquipFacStaSS00Entity) list.get(i))
                    .getMainConsequences());

                ss00Ex.setExiengtechnicalMeasures(((TEquipFacStaSS00Entity) 
                    list.get(i)).getExiengtechnicalMeasures());
                ss00Ex.setEximanagementMeasures(((TEquipFacStaSS00Entity) 
                    list.get(i)).getEximanagementMeasures());
                ss00Ex.setExitraieducatMeasures(((TEquipFacStaSS00Entity) 
                    list.get(i)).getExitraieducatMeasures());
                ss00Ex.setExipersprotecMeasures(((TEquipFacStaSS00Entity) 
                    list.get(i)).getExipersprotecMeasures());
                ss00Ex.setExiemergencyMeasures(((TEquipFacStaSS00Entity) 
                    list.get(i)).getExiemergencyMeasures());
                
                ss00Ex.setOneExiRiskValue(((TEquipFacStaSS00Entity) 
                    list.get(i)).getOneExiRiskValue());
                ss00Ex.setTwoExiRiskValue(((TEquipFacStaSS00Entity) 
                    list.get(i)).getTwoExiRiskValue());
                ss00Ex.setThreeExiRiskValue(((TEquipFacStaSS00Entity) 
                    list.get(i)).getThreeExiRiskValue());
                ss00Ex.setExiRiskLevel(((TEquipFacStaSS00Entity) 
                    list.get(i)).getExiRiskLevel());
                
                ss00ExcelList.add(ss00Ex);
            }
            tEquipFacStatusService.addSS00ExcelFile(companyId,userId,ss00ExcelList);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorOf(UserResponseEnums.DATA_IMPORT_FAILED_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导入作业活动清单
     * @param file 上传文件
     * @return
     */
    @RequestMapping("/ImportZY00ExcelFile")
    @ResponseBody
    public ResultDTO ImportZY00ExcelFile(@RequestParam MultipartFile file) {
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        try{
            Object obj = TEquipFacStaZY00Entity.class.newInstance();
            List<Object> list = ExcelUtils.importBaseExcel1(file,
                "作业活动导入测试",obj);
            int index = 2;
            List<TEquipFacStaZY00Entity> ss00ExcelList = new ArrayList<>();
            for(int i=index;i<list.size();i++) {

                TEquipFacStaZY00Entity ss00Ex =
                    (TEquipFacStaZY00Entity)list.get(i);

                ss00Ex.setEquipNum(((TEquipFacStaZY00Entity) list.get(i)).getEquipNum());
                ss00Ex.setEquipType(((TEquipFacStaZY00Entity) list.get(i)).getEquipType());
                ss00Ex.setEquipName(((TEquipFacStaZY00Entity) list.get(i)).getEquipName());
                ss00Ex.setMode(((TEquipFacStaZY00Entity) list.get(i)).getMode());
                ss00Ex.setPositionId(((TEquipFacStaZY00Entity) list.get(i)).getPositionId());
                ss00Ex.setAdministerId(((TEquipFacStaZY00Entity) list.get(i)).getAdministerId());
                ss00Ex.setEquipSp(((TEquipFacStaZY00Entity) list.get(i)).getEquipSp());
                ss00Ex.setRemarks(((TEquipFacStaZY00Entity) list.get(i)).getRemarks());
                ss00Ex.setPicture(((TEquipFacStaZY00Entity) list.get(i)).getPicture());

                ss00Ex.setInspectionItems(((TEquipFacStaZY00Entity) list.get(i))
                    .getInspectionItems());
                ss00Ex.setInspectionStandard(((TEquipFacStaZY00Entity) list.get(i))
                    .getInspectionStandard());
                ss00Ex.setNonCompliance(((TEquipFacStaZY00Entity) list.get(i))
                    .getNonCompliance());

                ss00Ex.setExiengtechnicalMeasures(((TEquipFacStaZY00Entity)
                    list.get(i)).getExiengtechnicalMeasures());
                ss00Ex.setEximanagementMeasures(((TEquipFacStaZY00Entity)
                    list.get(i)).getEximanagementMeasures());
                ss00Ex.setExitraieducatMeasures(((TEquipFacStaZY00Entity)
                    list.get(i)).getExitraieducatMeasures());
                ss00Ex.setExipersprotecMeasures(((TEquipFacStaZY00Entity)
                    list.get(i)).getExipersprotecMeasures());
                ss00Ex.setExiemergencyMeasures(((TEquipFacStaZY00Entity)
                    list.get(i)).getExiemergencyMeasures());

                ss00Ex.setOneExiRiskValue(((TEquipFacStaZY00Entity)
                    list.get(i)).getOneExiRiskValue());
                ss00Ex.setTwoExiRiskValue(((TEquipFacStaZY00Entity)
                    list.get(i)).getTwoExiRiskValue());
                ss00Ex.setThreeExiRiskValue(((TEquipFacStaZY00Entity)
                    list.get(i)).getThreeExiRiskValue());
                ss00Ex.setExiRiskLevel(((TEquipFacStaZY00Entity)
                    list.get(i)).getExiRiskLevel());

                ss00ExcelList.add(ss00Ex);
            }
            tEquipFacStatusService.addZY00ExcelFile(companyId,userId,ss00ExcelList);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorOf(UserResponseEnums.DATA_IMPORT_FAILED_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导入工艺节点清单
     * @param file 上传文件
     * @return
     */
    @RequestMapping("/ImportGY00ExcelFile")
    @ResponseBody
    public ResultDTO ImportGY00ExcelFile(@RequestParam MultipartFile file) {
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        try{
            Object obj = TEquipFacStaGY00Entity.class.newInstance();
            List<Object> list = ExcelUtils.importBaseExcel1(file,
                "工艺节点清单测试",obj);
            int index = 2;
            List<TEquipFacStaGY00Entity> ss00ExcelList = new ArrayList<>();
            for(int i=index;i<list.size();i++) {

                TEquipFacStaGY00Entity ss00Ex =
                    (TEquipFacStaGY00Entity)list.get(i);

                ss00Ex.setEquipNum(((TEquipFacStaGY00Entity) list.get(i)).getEquipNum());
                ss00Ex.setEquipType(((TEquipFacStaGY00Entity) list.get(i)).getEquipType());
                ss00Ex.setEquipName(((TEquipFacStaGY00Entity) list.get(i)).getEquipName());
                ss00Ex.setMode(((TEquipFacStaGY00Entity) list.get(i)).getMode());
                ss00Ex.setPositionId(((TEquipFacStaGY00Entity) list.get(i)).getPositionId());
                ss00Ex.setAdministerId(((TEquipFacStaGY00Entity) list.get(i)).getAdministerId());
                ss00Ex.setEquipSp(((TEquipFacStaGY00Entity) list.get(i)).getEquipSp());
                ss00Ex.setRemarks(((TEquipFacStaGY00Entity) list.get(i)).getRemarks());
                ss00Ex.setPicture(((TEquipFacStaGY00Entity) list.get(i)).getPicture());

                ss00Ex.setInspectionItems(((TEquipFacStaGY00Entity) list.get(i))
                    .getInspectionItems());
                ss00Ex.setInspectionStandard(((TEquipFacStaGY00Entity) list.get(i))
                    .getInspectionStandard());
                ss00Ex.setNonCompliance(((TEquipFacStaGY00Entity) list.get(i))
                    .getNonCompliance());

                ss00Ex.setExiengtechnicalMeasures(((TEquipFacStaGY00Entity)
                    list.get(i)).getExiengtechnicalMeasures());
                ss00Ex.setEximanagementMeasures(((TEquipFacStaGY00Entity)
                    list.get(i)).getEximanagementMeasures());
                ss00Ex.setExitraieducatMeasures(((TEquipFacStaGY00Entity)
                    list.get(i)).getExitraieducatMeasures());
                ss00Ex.setExipersprotecMeasures(((TEquipFacStaGY00Entity)
                    list.get(i)).getExipersprotecMeasures());
                ss00Ex.setExiemergencyMeasures(((TEquipFacStaGY00Entity)
                    list.get(i)).getExiemergencyMeasures());

                ss00Ex.setOneExiRiskValue(((TEquipFacStaGY00Entity)
                    list.get(i)).getOneExiRiskValue());
                ss00Ex.setTwoExiRiskValue(((TEquipFacStaGY00Entity)
                    list.get(i)).getTwoExiRiskValue());
                ss00Ex.setThreeExiRiskValue(((TEquipFacStaGY00Entity)
                    list.get(i)).getThreeExiRiskValue());
                ss00Ex.setExiRiskLevel(((TEquipFacStaGY00Entity)
                    list.get(i)).getExiRiskLevel());

                ss00ExcelList.add(ss00Ex);
            }
            tEquipFacStatusService.addGY00ExcelFile(companyId,userId,ss00ExcelList);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorOf(UserResponseEnums.DATA_IMPORT_FAILED_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
    
}
